package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormal2Activity;
import com.wolfalpha.utron.service.IdeaService;
import com.wolfalpha.utron.service.impl.IdeaServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;

/**
 * 创意详情界面
 * <p/>
 * Created by luodon on 15/8/27.
 */

public class IdeaPublishActivity extends BaseNormal2Activity {

    private EditText etWhat;
    private EditText etWhy;
    private EditText etHow;

    private ScrollView scrollView;
    private LinearLayout root;

    private int dir = 0;

    private IdeaService ideaService;

    boolean isPublishingFlag = false;//是否正在发布的标志

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("dir")) {
            UIHelper.showActivityAndFinish(IdeaPublishActivity.this, IdeaChooseDirActivity.class);
            finish();
            return;
        }

        dir = bundle.getInt("dir");
        ideaService = new IdeaServiceImpl();

        setContentView(R.layout.activity_idea_publish);

        intiView();
//        initListeners();
//        loadData();

    }

    private final static int IPUT_SHOW = 1;     //输入法打开
    private final static int IPUT_DISMISS = 2; //输入法隐藏

    private boolean ipuitShowing = false;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case IPUT_SHOW:
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(0, 0, 0, msg.arg1);
                    scrollView.setLayoutParams(params);
                    break;
                case IPUT_DISMISS:
                    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    params1.setMargins(0, 0, 0, 0);
                    scrollView.setLayoutParams(params1);
                    break;
            }

        }
    };

    /**
     * 初始化界面
     */
    private void intiView() {
        setTvTitle("分享我的灵感");
        setRightIcon(R.mipmap.app_finish);

        etWhat = (EditText) findViewById(R.id.et_idea_publish_what);
        etWhy = (EditText) findViewById(R.id.et_idea_publish_why);
        etHow = (EditText) findViewById(R.id.et_idea_publish_how);

        scrollView = (ScrollView) findViewById(R.id.sc_idea_publish);
        root = (LinearLayout) findViewById(R.id.ll_idea_publish_root);
//        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                                                                 @Override
//                                                                 public void onGlobalLayout() {
//                                                                     Rect rect = new Rect();
//                                                                     //获取root在窗体的可视区域
//                                                                     root.getWindowVisibleDisplayFrame(rect);
//                                                                     //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
//                                                                     final int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
//                                                                     //若不可视区域高度大于100，则键盘显示
//                                                                     if (rootInvisibleHeight > 0 && !ipuitShowing) {
//
//                                                                         System.out.println(rootInvisibleHeight);
//                                                                         Message message = new Message();
//                                                                         message.what = IPUT_SHOW;
//                                                                         message.arg1 = rootInvisibleHeight;
//                                                                         handler.sendMessage(message);
//                                                                     } else {
//                                                                         if (ipuitShowing)
//                                                                             handler.sendEmptyMessage(IPUT_DISMISS);
//                                                                     }
//                                                                 }
//                                                             }
//
//        );
    }


    @Override
    public void rightOperation() {
        if (isPublishingFlag) return;
        isPublishingFlag = true;

        final String what = etWhat.getText().toString().trim();
        final String why = etWhy.getText().toString().trim();
        final String how = etHow.getText().toString().trim();
        /**
         * 是什么和为什么必须填写
         */
        if (TextUtils.isEmpty(etWhat.getText().toString().trim()) || TextUtils.isEmpty(etWhy.getText().toString().trim())) {
            UIHelper.ToastMessage(IdeaPublishActivity.this, "请先完善信息");
            isPublishingFlag = false;
            return;
        }

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                final boolean result = ideaService.publish(what, how, why, dir);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result) {
                            UIHelper.ToastMessage(IdeaPublishActivity.this, "发布成功");
                            finish();
                        } else {
                            UIHelper.ToastMessage(IdeaPublishActivity.this, "发布失败,请重试");
                        }
                        isPublishingFlag = false;
                    }
                });

            }
        });
    }

}
